package cn.lili.controller.system;

import cn.lili.common.aop.annotation.DemoSite;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.other.service.PageDataService;
import cn.lili.modules.page.entity.dos.PageData;
import cn.lili.modules.page.entity.dto.PageDataSaveDTO;
import cn.lili.modules.page.entity.dto.PageDataSearchParams;
import cn.lili.modules.page.entity.dto.PageDataShowParams;
import cn.lili.modules.page.entity.vos.PageDataVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 页面设置管理接口
 *
 * @author paulGao
 * @since 2020-05-06 15:18:56
 */
@RestController
@Api(tags = "楼层装修接口")
@RequestMapping("/system/page")
@RequiredArgsConstructor
@Validated
public class PageDataController {

    private final PageDataService pageDataService;

    @ApiOperation(value = "C端获取页面信息")
    @GetMapping(value = "/show")
    public ResultMessage<PageDataVO> getPageData(PageDataShowParams pageDataShowParams) {
        return ResultUtil.data(pageDataService.getPageData(pageDataShowParams));
    }

    @ApiOperation(value = "获取页面信息")
    @ApiImplicitParam(name = "id", value = "页面ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "/{id}")
    public ResultMessage<PageData> getPageData(@PathVariable String id) {
        PageData pageData = pageDataService.getById(id);
        return ResultUtil.data(pageData);
    }

    @ApiOperation(value = "添加页面")
    @PostMapping
    public ResultMessage<PageData> addPageData(@Valid PageDataSaveDTO pageData) {
        return ResultUtil.data(pageDataService.addPageData(pageData));
    }

    @ApiOperation(value = "修改页面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "页面ID", required = true, dataType = "String", paramType = "path")
    })
    @DemoSite
    @PutMapping("/{id}")
    public ResultMessage<PageData> updatePageData(@Valid PageDataSaveDTO pageData, @NotNull @PathVariable String id) {
        pageData.setId(id);
        return ResultUtil.data(pageDataService.updatePageData(pageData));
    }

    @ApiOperation(value = "页面列表")
    @GetMapping
    public ResultMessage<Page<PageData>> pageDataList(PageVO pageVO, PageDataSearchParams pageDataSearchParams) {
        return ResultUtil.data(pageDataService.getPageDataList(pageVO, pageDataSearchParams));
    }

    @ApiOperation(value = "发布首页")
    @ApiImplicitParam(name = "id", value = "页面ID", required = true, dataType = "String", paramType = "path")
    @PutMapping("/release/{id}")
    @DemoSite
    public ResultMessage<PageData> release(@PathVariable String id) {
        return ResultUtil.data(pageDataService.releaseIndex(id));
    }

    @ApiOperation(value = "更新页面状态")
    @ApiImplicitParam(name = "id", value = "页面ID", required = true, dataType = "String", paramType = "path")
    @PutMapping("/status/{id}")
    @DemoSite
    public ResultMessage<PageData> updateStatus(@PathVariable String id, Boolean enable) {
        return ResultUtil.data(pageDataService.updateStatus(id, enable));
    }

    @ApiOperation(value = "删除页面")
    @DemoSite
    @ApiImplicitParam(name = "id", value = "页面ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public ResultMessage<Object> delete(@PathVariable String id) {
        pageDataService.deletePageData(id);
        return ResultUtil.success();
    }
}
